class Solution {
public:
    int sumOfBeauties(vector<int>& nums)
    {
        int n = nums.size();
        int ans = 0;
        int premax = nums[0];
        vector<int> suf_min(n);
        suf_min[n - 1] = nums.back();
        for (int i = n - 2; i >= 1; i--) suf_min[i] = min(suf_min[i + 1], nums[i]);
        for (int i = 1; i <= n - 2; i++)
        {
            if (nums[i] < suf_min[i + 1] && nums[i] > premax) ans += 2;
            else if (nums[i] > nums[i - 1] && nums[i] < nums[i + 1]) ans++;
            premax = max(premax, nums[i]);
        }
        return ans;
    }
};